package com.changhongit.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.changhongit.entity.Products;
import com.changhongit.service.ProductsService;
import com.changhongit.util.Common;
import com.changhongit.util.PageView;

/**
 * 
 * @author  JacksonYang
 * 2014-2-13
 * @Email: 
 * @version 1.0v
 */
@Controller
@RequestMapping("/background/product/")
public class ProductController {
	@Autowired
	private ProductsService productsService;
	/**
	 * @param model
	 * 存放返回界面的model
	 * @return
	 */
	@RequestMapping("query")
	public String query(Model model, Products products, String pageNow) {
		PageView pageView = null;
		if (Common.isEmpty(pageNow)) {
			pageView = new PageView(1);
		} else {
			pageView = new PageView(Integer.parseInt(pageNow));
		}
		pageView = productsService.query(pageView, products);
		model.addAttribute("pageView", pageView);
		return "/background/product/list";
	}

	/**
	 * 保存数据
	 * 
	 * @param model
	 * @param videoType
	 * @return
	 */
	@RequestMapping("add")
	public String add(Model model, Products products) {
		productsService.add(products);
		return "redirect:query.html";
	}

	/**
	 * 跑到新增界面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("addUI")
	public String addUI() {
		return "/background/product/add";
	}

	/**
	 * 删除
	 * 
	 * @param model
	 * @param videoTypeId
	 * @return
	 */
	@RequestMapping("deleteById")
	public String deleteById(Model model, String id) {
		productsService.delete(id);
		return "redirect:query.html";
	}

	/**
	 * 修改界面
	 * 
	 * @param model
	 * @param videoTypeIds
	 * @return
	 */
	@RequestMapping("getById")
	public String getById(Model model, String id, int type) {
		Products product = productsService.getById(id);
		model.addAttribute("product", product);
		if (type == 1) {
			return "/background/product/edit";
		} else {
			return "/background/product/show";
		}
	}

	/**
	 * 更新类型
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("update")
	public String update(Model model, Products product) {
		productsService.modify(product);
		if(product.getId()!=null)
			productsService.saveProduct(product);
		return "redirect:query.html";
	}

	/**
	 * 删除所选的
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("deleteAll")
	public String deleteAll(Model model, String[] check) {
		for (String string : check) {
			productsService.delete(string);
		}
		return "redirect:query.html";
	}
	
}